//
//  RVNavigationTitleView.m
//  Rockval
//
//  Created by TuanNT on 4/28/14.
//  Copyright (c) 2014 TNL. All rights reserved.
//

#import "RVNavigationTitleView.h"

@protocol RVNavigationTitleContentViewDelegate <NSObject>
- (void)drawContent:(CGRect)rect;
@end

@interface RVNavigationTitleContentView : UIView
@property (nonatomic, weak) id <RVNavigationTitleContentViewDelegate> delegate;
@end

@implementation RVNavigationTitleContentView
- (void)drawRect:(CGRect)rect {
	if ([self.delegate respondsToSelector:@selector(drawContent:)]) {
		[self.delegate drawContent:rect];
	}
}

@end

@interface RVNavigationTitleView () <RVNavigationTitleContentViewDelegate>
@property (nonatomic, strong) RVNavigationTitleContentView *contentView;
@end

@implementation RVNavigationTitleView

- (id)initWithCoder:(NSCoder *)aDecoder {
	self = [super initWithCoder:aDecoder];
	if (self) {
		[self setupContentView];
	}
	return self;
}

- (id)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		[self setupContentView];
	}
	return self;
}

- (void)setupContentView {
	self.contentView = [[RVNavigationTitleContentView alloc] initWithFrame:self.frame];
	self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
	self.contentView.delegate = self;
	[self addSubview:self.contentView];
	self.backgroundColor = [UIColor clearColor];
	self.contentView.backgroundColor = [UIColor clearColor];
	self.clipsToBounds = YES;
}

- (void)setNavigationBarTitle:(NSString *)navigationBarTitle {
	if (![_navigationBarTitle isEqualToString:navigationBarTitle]) {
		_navigationBarTitle = navigationBarTitle;
		[self.contentView setNeedsDisplay];
	}
}

- (void)setNavigationBarSubtitle:(NSString *)navigationBarSubtitle {
	if (![_navigationBarSubtitle isEqualToString:navigationBarSubtitle]) {
		if (navigationBarSubtitle.length && !_navigationBarSubtitle.length) {
			CATransition *transition = [CATransition animation];
			transition.duration = 0.4f;
			transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
			transition.type = kCATransitionPush;
			transition.subtype = kCATransitionFromTop;
			[transition setValue:(id)kCFBooleanFalse forKey:kCATransitionFade];
			[self.contentView.layer addAnimation:transition forKey:nil];
		}
		else if (!navigationBarSubtitle.length && _navigationBarSubtitle.length) {
			CATransition *transition = [CATransition animation];
			transition.duration = 0.4f;
			transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
			transition.type = kCATransitionPush;
			transition.subtype = kCATransitionFromBottom;
			[transition setValue:(id)kCFBooleanFalse forKey:kCATransitionFade];
			[self.contentView.layer addAnimation:transition forKey:nil];
		}
		_navigationBarSubtitle = navigationBarSubtitle;
		[self.contentView setNeedsDisplay];
	}
}

- (UIFont *)navigationBarTitleFont {
	if (!_navigationBarTitleFont) {
		_navigationBarTitleFont = [UIFont boldSystemFontOfSize:17];
	}
	return _navigationBarTitleFont;
}

- (UIFont *)navigationBarSubtitleFont {
	if (!_navigationBarSubtitleFont) {
		_navigationBarSubtitleFont = [UIFont systemFontOfSize:13];
	}
	return _navigationBarSubtitleFont;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawContent:(CGRect)rect {
	// Drawing code
	if (self.navigationBarSubtitle.length) {
		CGRect titleRect = rect;
		titleRect.origin.y = 4;
		titleRect.size.height = 20;
		[self.navigationBarTitleFontColor setFill];
		/// Make a copy of the default paragraph style
		NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
		/// Set line break mode
		paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
		/// Set text alignment
		paragraphStyle.alignment = NSTextAlignmentCenter;
		[self.navigationBarTitle drawInRect:titleRect withAttributes:@{
		     NSForegroundColorAttributeName : [UIColor whiteColor],
		     NSFontAttributeName : self.navigationBarTitleFont,
		     NSParagraphStyleAttributeName: paragraphStyle
		 }];
		CGRect subtitleRect = rect;
		subtitleRect.origin.y = 24;
		subtitleRect.size.height = rect.size.height - 24;
		[self.navigationBarSubtitleFontColor setFill];
		[self.navigationBarSubtitle drawInRect:subtitleRect withAttributes:@{
		     NSForegroundColorAttributeName : [UIColor whiteColor],
		     NSFontAttributeName :self.navigationBarSubtitleFont,
		     NSParagraphStyleAttributeName: paragraphStyle
		 }];
	}
	else {
		CGRect titleRect = rect;
		titleRect.origin.y = (rect.size.height - 24.f) / 2.f;
		titleRect.size.height = 24.f;
		[self.navigationBarTitleFontColor setFill];
		/// Make a copy of the default paragraph style
		NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
		/// Set line break mode
		paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
		/// Set text alignment
		paragraphStyle.alignment = NSTextAlignmentCenter;
		[self.navigationBarTitle drawInRect:titleRect withAttributes:@{
		     NSForegroundColorAttributeName : [UIColor whiteColor],
		     NSFontAttributeName : self.navigationBarTitleFont,
		     NSParagraphStyleAttributeName: paragraphStyle
		 }];
	}
}

@end
